package com.xbn.netty.nio;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.nio.MappedByteBuffer;
import java.nio.channels.FileChannel;

/**
 * @author XBN
 * @date 2020/10/14 8:42
 * @email 78742541@qq.com
 */
/*
* MappedByteBuffer可以让文件直接再内存中（堆外内存）修改，操作系统并不需要进行一次拷贝
* */
public class MappedByteBufferDemo {
    public static void main(String[] args) throws IOException {
        RandomAccessFile randomAccessFile = new RandomAccessFile("1.txt", "rw");
        //获取对应的通道
        FileChannel channel = randomAccessFile.getChannel();
        //参数1 读写模式   参数2，操作的起始位置    参数3 将1.txt的多少个字节映射到内存
        MappedByteBuffer mappedByteBuffer = channel.map(FileChannel.MapMode.READ_WRITE, 0, 5);
         //修改第一个字节的数据
        mappedByteBuffer.put(0, (byte) 'h');
        mappedByteBuffer.put(3, (byte) '9');
        randomAccessFile.close();

    }
}
